/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.basic.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import me.zhengjie.modules.account.mapper.AccountCommonMapper;
import me.zhengjie.modules.account.service.AccountAc50Service;
import me.zhengjie.modules.basic.domain.BasicAc01;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.basic.service.BasicAc01Service;
import me.zhengjie.modules.basic.domain.dto.BasicAc01QueryCriteria;
import me.zhengjie.modules.basic.mapper.BasicAc01Mapper;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
 * @author ly
 * @date 2025-04-24
 **/
@Service
@RequiredArgsConstructor
public class BasicAc01ServiceImpl extends ServiceImpl<BasicAc01Mapper, BasicAc01> implements BasicAc01Service {

    private final BasicAc01Mapper basicAc01Mapper;
    private final SequenceService sequenceService;
    private final AccountCommonMapper accountCommonMapper;
    @Resource
    BusinessLogService businessLogService;

    @Override
    public PageResult<BasicAc01> queryAll(BasicAc01QueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(basicAc01Mapper.findAll(criteria, page));
    }

    @Override
    public List<BasicAc01> queryAll(BasicAc01QueryCriteria criteria) {
        return basicAc01Mapper.findAll(criteria);
    }

    @Override
    public List<BasicAc01> getPersonInfo(BasicAc01 criteria){
        return basicAc01Mapper.getPersonInfo(criteria);
    }

    @Override
    public List<BasicAc01> getAc01(BasicAc01 criteria){
        return basicAc01Mapper.getAc01(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BasicAc01 resources) throws IOException {
        // 校验
        check(resources);
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_RYJCXXXZ);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        // 属组装
        Sequence sequence = new Sequence();
        sequence.setName("aac001");
        resources.setAac001(sequenceService.nextSequence(sequence));
        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
        resources.setAac327(resources.getAac006());

        basicAc01Mapper.insertAc01(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BasicAc01 resources) throws IOException {
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_RYJCXXbg);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);

        basicAc01Mapper.updateByAac001(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        basicAc01Mapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<BasicAc01> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BasicAc01 basicAc01 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("身份证号码", basicAc01.getAac002());
            map.put("姓名", basicAc01.getAac003());
            map.put("性别", basicAc01.getAac004());
            map.put("民族", basicAc01.getAac005());
            map.put("出生日期", basicAc01.getAac006());
            map.put("档案出生日期", basicAc01.getAac327());
            map.put("证件类型", basicAc01.getAac058());
            map.put("参工日期", basicAc01.getAac007());
            map.put("备注", basicAc01.getAae013());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 前置校验
     */
    public void check(BasicAc01 basicAc01) {
        // 非空校验
        ValidateUtil.isEmptyThrowException(basicAc01.getAac002(),"证件号码");
        ValidateUtil.isEmptyThrowException(basicAc01.getAac004(),"性别");
        ValidateUtil.isEmptyThrowException(basicAc01.getAac006(),"出日期");
        //证件号码重复校验
        BasicAc01 param = new BasicAc01();
        param.setAac002(basicAc01.getAac002());
        List<BasicAc01> ac01s = basicAc01Mapper.selectByAac002(param);
        if (!ValidateUtil.isEmpty(ac01s)) {
            throw new RuntimeException("【" + basicAc01.getAac002() + "】人员基础信息已存在，直接修改即可");
        }
    }

    @Override
    public List<Map> getAc01Ac02(Map map) {
        return accountCommonMapper.queryAc01Ac02(map);

    }
}
